package com.fengze.shop.common.config;

import com.fengze.shop.common.cache.ConfigConstant;
import org.jdom.Element;

import java.io.Serializable;

/**
 * 配置项为单个的时候使用
 *
 * @author zhanglingsi
 */
public class Item extends AbstractItem implements Serializable {

    private static final long serialVersionUID = -1270818045758491816L;

    public Item() {
        this.element = new Element(ConfigConstant.CONFIG_ITEM);
    }

    public Item(Element element) {
        this.element = element;
    }

    public Item(String name) {
        this();
        this.element.addContent(new Element(ConfigConstant.NAME));
        this.element.getChild(ConfigConstant.NAME).setText(name);
    }

    public Item(String name, String description) {
        this(name);
        this.element.addContent(new Element(ConfigConstant.DESCRIPTION));
        this.element.getChild(ConfigConstant.DESCRIPTION).setText(description);
    }

    public String getValue() {
        String value = element.getChild(ConfigConstant.VALUE).getText();

        return value == null ? "" : value;
    }

    public void setValue(String value) {
        this.addElement(ConfigConstant.VALUE, value);
    }

    public void modify(Item item) {
        this.setDescription(item.getDescription());
        this.setValue(item.getValue());
    }
}
